package com.wd.tech.activity.adaptert;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.wd.tech.R;
import com.wd.tech.activity.bean.RecommendationDisplayBean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import io.reactivex.Observer;

/**
 * @author 时文豪
 * @description:首页详情展示
 * @date :2020/7/6 11:30
 */
public class RecommendationDisplayAdaptert extends XRecyclerView.Adapter<XRecyclerView.ViewHolder> {
    Context context;
    List<RecommendationDisplayBean.ResultBean> list;
    private setOnClicked onClicked;

    public RecommendationDisplayAdaptert(Context context, List<RecommendationDisplayBean.ResultBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = View.inflate(context, R.layout.recommendationdisplay_item, null);
        return new MyRecommend(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ((MyRecommend) holder).txt_one.setText(list.get(position).getTitle());
        ((MyRecommend) holder).txt_two.setText(list.get(position).getSummary());
        Glide.with(context).load(list.get(position).getThumbnail()).apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))
                .into(((MyRecommend) holder).iv);
        String format = new SimpleDateFormat("yy-MM-dd")
                .format(new Date(list.get(position).getReleaseTime()));
        ((MyRecommend) holder).item_time.setText(format);
        ((MyRecommend) holder).item_comeforom.setText(list.get(position).getSource());
        ((MyRecommend) holder).item_num.setText(list.get(position).getCollection() + "");
        ((MyRecommend) holder).item_ch.setText(list.get(position).getShare() + "");
        ((MyRecommend) holder).txt_two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClicked.OnClick(list.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void onClick(setOnClicked Click) {
        onClicked = Click;
    }

    public interface setOnClicked {
        void OnClick(RecommendationDisplayBean.ResultBean resultBean);
    }

    class MyRecommend extends XRecyclerView.ViewHolder {

        private final TextView txt_one;
        private final TextView txt_two;
        private final TextView item_comeforom;
        private final ImageView iv;
        private final TextView item_ch;
        private final TextView item_num;
        private final TextView item_time;

        public MyRecommend(@NonNull View itemView) {
            super(itemView);
            txt_one = itemView.findViewById(R.id.item_txt_one);
            txt_two = itemView.findViewById(R.id.item_txt_two);
            item_comeforom = itemView.findViewById(R.id.item_comefrom);
            iv = itemView.findViewById(R.id.item_iv);
            item_ch = itemView.findViewById(R.id.item_char);
            item_num = itemView.findViewById(R.id.item_num);
            item_time = itemView.findViewById(R.id.item_time);
        }
    }
}
